// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ICache from "../cache/ICache";
import { MONITOR_LIST, selfCls } from "../common/Decorator";

const {ccclass, property} = cc._decorator;

@selfCls
export default class CCMP extends cc.Component {
    private _isRunning:boolean = false;
    public get isRunning() {
        return this._isRunning;
    }
    protected set isRunning(val:boolean) {
        if (!this.isRunning && val) {
            this.onRunning();
        }
        this._isRunning = val;
    }
    protected update(dt: number): void {
        if (this.isRunning) {
            this.onUpdate(dt);
        }
    }

    protected onLoad(): void {
        this.initMonitor();
    }
    protected onDestroy(): void {
        this.closeMonitor();
        this.onStop();
    }

    protected initMonitor() {
        let list:[] = this[MONITOR_LIST] || [];
        for (let i = 0; i < list.length; i++) {
            list[i] && (list[i] as Function)(this);
        }
    }

    protected closeMonitor() {
        let list:[] = this[MONITOR_LIST] || [];
        for (let i = 0; i < list.length; i++) {
            list[i] && (list[i] as Function)(this, null, true);
        }
    }

    async init(args?) {}

    onRunning() {}
    onUpdate(dt) {}
    onStop() {}

}
